/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.utils;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.toedo.logging.Log;

public class ApplicationUtils {
	
	public static void closeViewByID(String id) {
		List<IViewReference> viewList = new ArrayList<IViewReference>();
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewReference[] tabView = page.getViewReferences();
		
		for (int i = 0; i < tabView.length; i++) {
			if ((tabView[i].getId() != null) &&
					(tabView[i].getId().equals(id))) {
				viewList.add(tabView[i]);
			}
		}
		
		if (viewList.size() > 0) {
			for (IViewReference view : viewList) {
				page.hideView(view);
			}
		}
	}
	
	public static void closeViewByID(String primaryId, String secondaryId) {
		IViewReference view = null;
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewReference[] tabView = page.getViewReferences();
		
		for (int i = 0; i < tabView.length; i++) {
			if ((tabView[i].getId() != null) &&
					(tabView[i].getSecondaryId() != null) &&
					(tabView[i].getId().equals(primaryId))&&
					(tabView[i].getSecondaryId().equals(secondaryId))) {
				view = tabView[i];
				break;
			}
		}
		
		if (view != null) {
			page.hideView(view);
		}
	}
	
	public static void switchViewVisibleStateById(String id) {
		IViewReference view = null;
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IViewReference[] tabView = page.getViewReferences();
		
		for (int i = 0; i < tabView.length; i++) {
			if ((tabView[i].getId() != null) &&
					(tabView[i].getId().equals(id))) {
				view = tabView[i];
				break;
			}
		}
		
		if (view != null) {
			page.hideView(view);
		} else {
			try {
				page.showView(id);
				
			} catch (PartInitException e) {
				Log.getInstance(ApplicationUtils.class).warn(String.format("Unable to open view: %s", id)); //$NON-NLS-1$
			}
		}
	}
	
	public static void showViewByID(String id) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();							
		try {			
			page.showView(id);
			
		} catch (PartInitException e) {
			Log.getInstance(ApplicationUtils.class).warn(String.format("Unable to open view: %s", id)); //$NON-NLS-1$
		}
	}
	
	public static void showViewByID(String id, String secondaryId) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();							

		try {			
			page.showView(id, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
			
		} catch (PartInitException e) {
			Log.getInstance(ApplicationUtils.class).warn(String.format("Unable to open view: %s:%s", id, secondaryId)); //$NON-NLS-1$
		}
	}
	
	public static void notifyUser(String title, String message) {
		MessageDialog.openWarning(Display.getDefault().getActiveShell(), title, message);
	}
	
	public static void openLinkInBrowser(String url, boolean notifyOnError) {
		try {
			
			Desktop.getDesktop().browse(new URI(url));
			
		} catch (IOException e) {
			String message = String.format("Unable to open link: IOException: %s", e.getMessage()); //$NON-NLS-1$
			Log.getInstance(ApplicationUtils.class).warn(message);
			
			if (notifyOnError) {
				notifyUser("Error while openning link", message); //$NON-NLS-1$
			}
		} catch (URISyntaxException e) {
			String message = String.format("Unable to open link: URISyntaxException: %s", e.getMessage()); //$NON-NLS-1$
			Log.getInstance(ApplicationUtils.class).warn(message);
			
			if (notifyOnError) {
				notifyUser("Error while openning link", message); //$NON-NLS-1$
			}
		}
	}

}
